<% @main_header = "#{link_to('Ptrack:', '/')} #{@project.name if @project}: Contacts" %>

<div style="color:red;">
  <%= error_messages_for :contact %>
</div>

<% form_for :contact, @contact, :url => {:action => 'create'} do |f| %>
  <div style="float: left; width: 100%;">
    <fieldset>
      <legend>Contact Information</legend>
      <table>

        <% if @contacts %>
        <tr>
          <td colspan="4">
          <div style="background-color: #efefef; padding: 4px">
            Import from other contact:
            <%# This part is for populating selectbox %>
            <script type="text/javascript">
              var mainContacts = new Array;
              <%- i=0; @contacts.each do |contact| -%>
              mainContacts[<%=i%>] = <%= contact.to_json(:except => [ :created_at, :updated_at, :id ]) %>;
              <%-
                i+=1
                end
              -%>
               <%# Auxilary function for import %>
               function mainContactImport(id) {
                 if(id > -1) {
                   var contact = mainContacts[id].contact;
                   $.each(contact, function(k,v) {
                     $('#contact_'+k).val(v);
                   });
                   if(contact.inhouse == 1) {
                    $('#contact_inhouse').attr({checked: 'checked'});
                   } else {
                     $('#contact_inhouse').removeAttr('checked');
                   }
                 } else {
                   $.each(mainContacts[0].contact, function(k,v) {
                     $('#contact_'+k).val('');
                   });
                   $('#contact_inhouse').removeAttr('checked');
                 }
               }
            </script>
            <select name="main_contact_id" id="eMainContact" onchange="mainContactImport(parseInt($('#eMainContact').val()))">
              <option value="-1">Contacts List</option>
              <%- i=0; @contacts.each do |contact| -%>>
                <option value="<%=i%>"><%= contact.full_name %> <%= " (#{contact.project.name})" if contact.project %></option>
              <%- i+=1; end -%>
            </select>
          </div>
          </td>
        </tr>
        <% end %>

        <tr>
          <td>
            <%= f.label :name, 'Name:'%>
          </td>
          <td>
            <%= f.text_field :name, :tabindex => "1"  %>
          </td>
          <td>
            <%= f.label :company, 'Company:' %>
          </td>
          <td>
            <% if @contact.inhouse %>
              <%= f.text_field :company, :disabled => true %>
            <%else%>
              <%= f.text_field :company, {:disabled => false, :tabindex => "7"} %>
            <%end%>
          </td>
        </tr>
        <tr>
          <td>
            <%= f.label :surname, 'Surname:' %>
          </td>
          <td>
            <%= f.text_field :surname , :tabindex => "2" %>
          </td>
          <td>
            <%= f.label :profession, 'Profession:' %>
          </td>
          <td>
            <%= f.text_field :profession, :tabindex => "8" %>
          </td>

        </tr>
        <tr>
          <td>
            <%= f.label :phone, 'Phone Number:' %>
          </td>
          <td>
            <%= f.text_field :phone, :tabindex => "3" %>
          </td>
          <td>
            <%= f.label :inhouse, 'Inhouse Personnel:' %>
          </td>
          <td>
            <%= f.check_box :inhouse, :onchange => "javascript:inhousetoggle()", :tabindex => "9"%>
          </td>
        </tr>
        <tr>
          <td>
            <%= f.label :fax, 'Fax Number:' %>
          </td>
          <td>
            <%= f.text_field :fax, :tabindex => "4" %>
          </td>
          <td>
            <%= f.label :email, 'E-mail Address:' %>
          </td>
          <td>
            <%= f.text_field :email, :tabindex => "10" %>
          </td>
        </tr>
        <tr>
          <td>
            <%= f.label :mobile, 'Mobile Phone Number:' %>
          </td>
          <td>
            <%= f.text_field :mobile, :tabindex => "5" %>
          </td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td style="vertical-align:top;">
            <%= f.label :address, 'Address:' %>
          </td>
          <td>
            <%= f.text_area :address, {:tabindex => "6", :style => "width:193px; height:80px;" } %>
          </td>
          <td></td>
          <td></td>
        </tr>
      </table>
    </fieldset>
  </div>
  <br/>
  <table style="width: 100%;">
    <tr>
      <td style="text-align: right; width: 50%;">
        <%= f.submit "Save Contact", :tabindex => "11" %>
      </td>
    <% end %>

    <td style="text-align: left; width: 50%;">
      <%= button_to "Cancel", {:action => :index}, {:method => :get, :tabindex => "12"} %>
    </td>
  </tr>
</table>

<script language="JavaScript">
  function inhousetoggle()
  {
    var companyTb = document.getElementById('contact_company');
    var inhouseCb = document.getElementById('contact_inhouse');
		
    if (inhouseCb.checked)
      companyTb.value = "Inhouse Personnel";
    else
      companyTb.value = "";
		
    companyTb.disabled = inhouseCb.checked;
  }
</script>
